<?php
/**
 * Created by PhpStorm.
 * Project: xuxiaoming
 * Date: 2017/11/7
 * Time: 17:55
 */

namespace App\Http\Controllers;


use App\Models\Module;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use App\Models\UserProject;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;

class ProjectController extends Controller
{
    /**
     * 列表
     * @return string
     */
    public function index()
    {
        $projects = Project::all();
        if ($projects) {
            return sendJsonData($projects->toArray());
        } else {
            return sendJsonMessage('数据不存在', true);
        }
    }


    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getProject(Request $request, $id)
    {
        $project = Project::find($id);
        if ($project) {
            return sendJsonData($project->toArray());
        } else {
            return sendJsonMessage('数据不存在', true);
        }
    }

    /**
     * 列表
     *
     * @param Request $request 请求
     * @param $id ID
     * @return string
     */
    public function getProjectModules(Request $request, $id)
    {
        $modules = Module::where(["project_id" => $id])->with("tasks")->get();
        if ($modules) {
            return sendJsonData($modules->toArray());
        } else {
            return sendJsonMessage('数据不存在', true);
        }
    }

    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getProjectTasks(Request $request, $id)
    {
        $task = Task::where(["project_id" => $id])->get();
        if ($task) {
            return sendJsonData($task->toArray());
        } else {
            return sendJsonMessage('数据不存在', true);
        }
    }


    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getProjectUsers(Request $request, $id)
    {
        $userProject = UserProject::where(["project_id" => $id])->with("users")->get();
        if ($userProject) {
            return sendJsonData($userProject->toArray());
        } else {
            return sendJsonMessage('数据不存在', true);
        }
    }

    /**
     * 创建用户
     * @param Request $request
     * @return string
     */
    public function createProject(Request $request)
    {
        $projects = Project::create($request->all());
        return sendJsonData($projects->toArray());
    }

    /**
     * 更新用户
     *
     * @param Request $request
     * @param $id
     * @return string
     */
    public function updateProject(Request $request, $id)
    {
        $projects = Project::find($id);
        $projects->title = $request->input('title');
        $projects->save();
        return sendJsonData($projects->toArray());
    }

    /**
     * 删除用户
     *
     * @param $id
     * @return string
     */
    public function deleteProject($id)
    {
        $projects = Project::find($id);
        if ($projects) {
            $projects->delete();
            return sendJsonMessage('删除成功');
        } else {
            return sendJsonMessage('数据不存在', true);
        }

    }


}